#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""X Tile main module"""

#      x-tile
#      
#      Copyright 2009-2011
#      Giuseppe Penone <giuspen@gmail.com>,
#      Chris Camacho (chris_c) <codifies@gmail.com>.
#      
#      plus many thanks to  http://tronche.com/gui/x/xlib/
#                      and  http://tripie.sweb.cz/utils/wmctrl/
#
#      This program is free software; you can redistribute it and/or modify
#      it under the terms of the GNU General Public License as published by
#      the Free Software Foundation; either version 2 of the License, or
#      (at your option) any later version.
#      
#      This program is distributed in the hope that it will be useful,
#      but WITHOUT ANY WARRANTY; without even the implied warranty of
#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#      GNU General Public License for more details.
#      
#      You should have received a copy of the GNU General Public License
#      along with this program; if not, write to the Free Software
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#      MA 02110-1301, USA.

import sys, os, gettext, ctypes, ctypes.util, locale
import gtk, gconf
import __builtin__

if os.path.isfile('modules/globs.py'): MODULES_PATH = 'modules/'
else: MODULES_PATH = '/usr/share/x-tile/modules/'
sys.path.append(MODULES_PATH)
import cons, globs, core

# language installation
gconf_client = gconf.client_get_default()
gconf_client.add_dir(cons.GCONF_DIR, gconf.CLIENT_PRELOAD_NONE)
lang_str = gconf_client.get_string(cons.GCONF_LANG)
if lang_str == None:
    gconf_client.set_string(cons.GCONF_LANG, "default")
    lang_str = "default"
if lang_str != "default": os.environ["LANGUAGE"] = lang_str
try:
    locale.bindtextdomain(cons.APP_NAME, cons.LOCALE_PATH)
    gettext.translation(cons.APP_NAME, cons.LOCALE_PATH).install()
except:
    def _(transl_str):
        return transl_str
    __builtin__._ = _

__builtin__.glob = globs.GlobalsObject()

try:
   libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library("libc"))
   libc.prctl(15, cons.APP_NAME, 0, 0, 0)
except: print "libc.prctl not available, the process name will be python and not x-tile"

if len(sys.argv) < 2: sys.argv.append("w")

arg = sys.argv[1]
if arg == "w":
    x = core.XTile(core.InfoModel())
    x.launch_application()
    x.reload_windows_list()
    gtk.main()
elif arg in cons.CMD_LINE_ACTIONS:
    x = core.XTile(core.InfoModel())
    x.launch_application()
    x.cmd_line_only = True
    if arg not in ["z", "i", "y"]:
        x.reload_windows_list()
        x.flag_all_rows()
    if arg == "z": x.undo_tiling()
    elif arg == "i": x.invert_tiling()
    elif arg == "y": x.cycle_tiling()
    elif arg == "v": x.tile_vertically()
    elif arg == "h": x.tile_horizontally()
    elif arg == "u": x.tile_triangle_up()
    elif arg == "d": x.tile_triangle_down()
    elif arg == "l": x.tile_triangle_left()
    elif arg == "r": x.tile_triangle_right()
    elif arg == "q": x.tile_quad()
    elif arg == "g":
        wins = len(x.store.get_checked_windows_list(True)[0])
        if wins == 1:
            x.maximize_checked_windows()
        elif wins:
            try:
                rows, cols = ([max(0, min(wins, int(a))) for a in sys.argv[2:4]] + [0, 0])[:2]
            except:
                print "bad arguments"
                exit(1)
            if not rows and not cols:
                for rows, cols in ((r, c) for r in xrange(1, 100) for c in [r, r + 1]):
                    if rows * cols >= wins:
                        break
            elif not rows:
                rows = (wins + cols - 1) // cols
            elif not cols:
                cols = (wins + rows - 1) // rows
            cons.GRID_ROWS, cons.GRID_COLS = rows, cols
            x.tile_grid()
    elif arg == "1": x.tile_custom_1_run()
    elif arg == "2": x.tile_custom_2_run()
    elif arg == "m": x.maximize_checked_windows()
    elif arg == "M": x.unmaximize_checked_windows()
    elif arg == "c": x.close_checked_windows()
else: # -h
    print cons.HELP_TEXT
